<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Ajax POST请求</title>
    <style>
      .box {
        width: 200px;
        height: 100px;
        border: 1px solid #903;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>

    <script>
      const box = document.querySelector('.box')
      box.addEventListener('mouseover', function () {
        // 1.创建对象
        const xhr = new XMLHttpRequest()

        // 2.设置请求方法和 url
        xhr.open('POST', 'http://localhost:9000/server')
        // 设置请求头
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')

        // 3.发送
        // xhr.send('a=100&b=200&c=300')
        xhr.send('a:100&b:200&c:300')

        // 4.处理服务器端返回的结果
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4 && xhr.status === 200) {
            box.innerHTML = xhr.response
          }
        }
      })
    </script>
  </body>
</html>
